package com.juejin.www.service.impl;

import com.juejin.www.dao.CommentDao;
import com.juejin.www.dao.LikeDao;
import com.juejin.www.model.DO.Like;
import com.juejin.www.model.DTO.CommentDTO;
import com.juejin.www.model.DTO.LikeDTO;
import com.juejin.www.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author Jacky
 */
@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeDao likeDao;

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean add(LikeDTO likeDTO) {
        if(likeDao.checkLike(likeDTO.getBeLikedId(),likeDTO.getBeLikedType(),likeDTO.getUserId())>0){
            return false;
        }
        Like like = new Like(null,
                likeDTO.getBeLikedId(),
                likeDTO.getBeLikedType(),
                likeDTO.getUserId(),
                LocalDateTime.now(),
                LocalDateTime.now());
        likeDao.add(like);
        if(likeDTO.getBeLikedType()==1){
            likeDao.addArticleLikeCount(likeDTO.getBeLikedId());
            if(stringRedisTemplate.opsForValue().get("article:"+likeDTO.getBeLikedId())!=null){
                stringRedisTemplate.delete("article:"+likeDTO.getBeLikedId());
            }
        }else if(likeDTO.getBeLikedType()==2){
            likeDao.addBoilingPointLikeCount(likeDTO.getBeLikedId());
            if(stringRedisTemplate.opsForValue().get("boilingPoints")!=null){
                stringRedisTemplate.delete("boilingPoints");
            }
        }else {
            likeDao.addCommentLikeCount(likeDTO.getBeLikedId());
            CommentDTO beCommented = commentDao.getBeCommented(likeDTO.getBeLikedId());
            if(stringRedisTemplate.opsForValue().get("comment:"+beCommented.getBeCommentedType()+":"+beCommented.getBeCommentedId())!=null){
                stringRedisTemplate.delete("comment:"+beCommented.getBeCommentedType()+":"+beCommented.getBeCommentedId());
            }
        }
        return true;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void cancelLike(LikeDTO likeDTO) {
        likeDao.cancelLike(likeDTO);
        if(likeDTO.getBeLikedType()==1){
            likeDao.reduceArticleLikeCount(likeDTO.getBeLikedId());
            if(stringRedisTemplate.opsForValue().get("article:"+likeDTO.getBeLikedId())!=null){
                stringRedisTemplate.delete("article:"+likeDTO.getBeLikedId());
            }
        }else if(likeDTO.getBeLikedType()==2){
            likeDao.reduceBoilingPointLikeCount(likeDTO.getBeLikedId());
            if(stringRedisTemplate.opsForValue().get("boilingPoints")!=null){
                stringRedisTemplate.delete("boilingPoints");
            }
        }else {
            likeDao.reduceCommentLikeCount(likeDTO.getBeLikedId());
            CommentDTO beCommented = commentDao.getBeCommented(likeDTO.getBeLikedId());
            if(stringRedisTemplate.opsForValue().get("comment:"+beCommented.getBeCommentedType()+":"+beCommented.getBeCommentedId())!=null){
                stringRedisTemplate.delete("comment:"+beCommented.getBeCommentedType()+":"+beCommented.getBeCommentedId());
            }
        }
    }
}
